package com.tianmu.demo.ad;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.tianmu.ad.InterstitialAd;
import com.tianmu.ad.bean.InterstitialAdInfo;
import com.tianmu.ad.error.TianmuError;
import com.tianmu.ad.listener.InterstitialAdListener;
import com.tianmu.utils.TianmuLogUtil;
import com.tianmudemo.R;
import com.tianmu.demo.constant.TianmuDemoConstant;

/**
 * @Description:
 * @Author: maipian
 * @CreateDate: 2021/9/27 5:18 PM
 */
public class InterstitialAdActivity extends AppCompatActivity {

    private InterstitialAd interstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interstitial);

        findViewById(R.id.btn_load_ad).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadInterstitialAd();
            }
        });
    }

    private void loadInterstitialAd() {
        interstitialAd = new InterstitialAd(this);
        interstitialAd.setListener(new InterstitialAdListener() {
            @Override
            public void onAdReceive(InterstitialAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdReceive");
                adInfo.showInterstitial(InterstitialAdActivity.this);
            }

            @Override
            public void onAdExpose(InterstitialAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdExpose");
            }

            @Override
            public void onAdClick(InterstitialAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdClick");
            }

            @Override
            public void onAdClose(InterstitialAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdClose");
            }

            @Override
            public void onAdFailed(TianmuError tianmuError) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdFailed error" + tianmuError.getError());
            }
        });
        interstitialAd.loadAd(TianmuDemoConstant.INTERSTITIAL_ID);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (interstitialAd != null) {
            interstitialAd.release();
            interstitialAd = null;
        }
    }
}
